package DataBase

import (
	"SalaryPayment/Model"
)

var PayrollDatabase = &payrollDatabase{map[int]*Model.Employee{}, map[int]int{}}

type payrollDatabase struct {
	itsEmployees    map[int]*Model.Employee
	itsUnionMembers map[int]int
}

func (pd *payrollDatabase) GetEmployee(empid int) *Model.Employee {
	return pd.itsEmployees[empid]
}

func (pd *payrollDatabase) AddEmployee(empid int, e *Model.Employee) {
	pd.itsEmployees[empid] = e
}

func (pd *payrollDatabase) Clear() {
	pd.itsUnionMembers = map[int]int{}
	pd.itsEmployees = map[int]*Model.Employee{}
}

func (pd *payrollDatabase) DeleteEmployee(empid int) {
	delete(pd.itsEmployees, empid)
}

func (pd *payrollDatabase) AddUnionMember(memberId int, employee *Model.Employee) {
	empid := employee.GetEmpid()
	pd.itsUnionMembers[memberId] = empid
}

func (pd *payrollDatabase) RemoveUnionMember(memberId int) {
	delete(pd.itsUnionMembers, memberId)
}

func (pd *payrollDatabase) GetUnionMember(memberId int) *Model.Employee {
	if empid, ok := pd.itsUnionMembers[memberId]; ok {
		return pd.itsEmployees[empid]
	}

	return nil
}

func (pd *payrollDatabase) GetAllEmployee() []int {
	var list []int

	for key := range pd.itsEmployees {
		list = append(list, key)
	}

	return list
}
